const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    findlist:[],
    id:'',
    senduser:'',
    user:'',
    findindex:'',
    weekarr:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
  },
  // 年月日
  formatDate: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    return year + '/' + month + '/' + day
  },
  // 年月日 时分秒
  formatDate1: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }
    var second = function () { //获取秒数
        return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    }
    return year + '/' + month + '/' + day + ' ' + hour() + ':' + minute() + ':' + second()
  },
  // 时分
  formatDate2: function (times) {
    var date = new Date(times);
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }
    return hour() + ':' + minute()
  },
  // 月日
  formatDate3: function (times) {
    var date = new Date(times);
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    return month + '月' + day + '日'
  },
  // 年月
  formatDate4: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    return year + '年' + month + '月'
  },
  onChange(e) {
    let date=new Date();
    let year = date.getFullYear()
    date=this.formatDate(date) //截取年月日
    date=this.formatDate1(date) //形成年月日 00：00：00
    this.setData({
      value: e.detail,
    });
    wx.request({
      url: `http://localhost:3000/message/searchchat?id1=${this.data.id}&id2=${this.data.senduser.id}&condition=${this.data.value}`,
      method:'GET',
      success:res=>{
        console.log(res.data)
        this.setData({
          findlist:res.data.list
        })
        let list = this.data.findlist
        for(let i=0;i<list.length;i++){
          let time = list[i].time //所有
          let date0 = new Date(time)
          let sendyear = date0.getFullYear()
          let newtime = this.formatDate(time) //截取年月日
          newtime = this.formatDate1(newtime) //年月日 00：00：00
          let date1 = new Date(date)
          let time1 = new Date(newtime)
          let day = parseInt((date1.getTime()-time1.getTime())/(1000*60*60*24))
          if(day==0){
            list[i].date = this.formatDate2(time)
          }else if(day == 1){
            list[i].date = '昨天'
          }else if(day <7 ){
            let time2 = new Date(time)
            let week = time2.getDay()
            list[i].date = this.data.weekarr[week]
          }else if(sendyear<year){
            list[i].date = this.formatDate4(time)
          }else{
            list[i].date = this.formatDate3(time)
          }
        }
        this.setData({
          findlist:list
        })
      },
      fail:err=>{
        console.error(err)
      }
    })
  },
  onSearch() {
    console.log(this.data.value);
    wx.request({
      url: `http://localhost:3000/message/searchchat?id1=${this.data.id}&id2=${this.data.senduser.id}&condition=${this.data.value}`,
      method:'GET',
      success:res=>{
        console.log(res.data)
        this.setData({
          findlist:res.data.list
        })
      },
      fail:err=>{
        console.error(err)
      }
    })
  },
  onClick() {
    console.log(this.data.value);
  },
  gotochat(e){
    // 获取跳转的记录
    let index = e.currentTarget.id
    let findmes = this.data.findlist[index]
    // 获取位置
    wx.request({
      url: `http://localhost:3000/message/gettalks?receiveid=${this.data.id}&sendid=${this.data.senduser.id}`,
      method:'GET',
      success:res=>{
        let arr = res.data.list
        for(let i=0;i<arr.length;i++){
          if(arr[i].id == findmes.id){
            this.setData({
              findindex:i
            })
            console.log(this.data.findindex)
            // 跳转
            wx.navigateTo({
              url: `/pages/talk/talk?receiveId=${this.data.id}&findindex=${this.data.findindex}&findflag=1`,
            })
            break;
          }
        }
      },
      fail:err=>{
        console.error(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id,
      senduser:app.globalData.user,
    })
    wx.request({
      url: `http://localhost:3000/message/getreceiveid?receiveid=${this.data.id}`,
      method:'GET',
      success:res=>{
        this.setData({
          user:res.data.user
        })
        console.log(this.data.user,this.data.senduser)
      },
      fail:err=>{
        console.error(err)
      }
    })
  },
})